package com.my.lucky.controller;

import com.my.lucky.model.FilterCondition;
import com.my.lucky.service.FilterConditionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/filter-conditions")
public class FilterConditionController {

    @Autowired
    private FilterConditionService filterConditionService;

    // 获取当前用户的所有过滤条件
    @GetMapping
    public List<FilterCondition> getFilterConditions() {
        // 实际应用中应从登录信息获取用户ID
        Long userId = 1L; // 临时硬编码，实际应从认证信息获取
        return filterConditionService.getFilterConditionsByUser(userId);
    }

    // 保存过滤条件
    @PostMapping
    public Map<String, Object> saveFilterCondition(@RequestBody FilterCondition condition) {
        Map<String, Object> result = new HashMap<>();
        try {
            // 实际应用中应从登录信息获取用户ID
            Long userId = 1L; // 临时硬编码
            condition.setUserId(userId);
            condition.setCreatedAt(new Date());
            System.out.println("condition==" + condition);
            FilterCondition saved = filterConditionService.saveFilterCondition(condition);
            result.put("success", true);
            result.put("data", saved);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "保存失败: " + e.getMessage());
        }
        return result;
    }

    // 获取单个过滤条件
    @GetMapping("/{id}")
    public FilterCondition getFilterCondition(@PathVariable Long id) {
        // 实际应用中应从登录信息获取用户ID
        Long userId = 1L; // 临时硬编码
        return filterConditionService.getFilterConditionByIdAndUser(id, userId);
    }

    // 删除过滤条件
    @DeleteMapping("/{id}")
    public Map<String, Object> deleteFilterCondition(@PathVariable Long id) {
        Map<String, Object> result = new HashMap<>();
        try {
            // 实际应用中应从登录信息获取用户ID
            Long userId = 1L; // 临时硬编码
            filterConditionService.deleteFilterCondition(id, userId);
            result.put("success", true);
            result.put("message", "删除成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "删除失败: " + e.getMessage());
        }
        return result;
    }
}

